package com.sense.kukoo.biz_me.view

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.sense.feel.common.utils.ToastUtils
import com.sense.feel.login.utils.ReadAssetsJsonUtil
import com.sense.kukoo.biz_me.R
import com.sense.kukoo.biz_me.adapter.SelectCountryAdapter
import com.sense.kukoo.biz_me.databinding.BizMeActivitySelectCountryBinding
import com.sense.kukoo.biz_me.vm.SelectCountryViewModel
import com.sense.kukoo.common.constant.ARouterUrl
import com.sense.kukoo.common.event.CountrySelectEvent
import com.sense.kukoo.common.mvvm.view.BaseMvvmActivity
import com.sense.kukoo.common.repo.bean.AreaBean
import com.sense.kukoo.lib_logger.LoggerManager
import org.greenrobot.eventbus.EventBus

@Route(path = ARouterUrl.ROUTE_ME_MODULE_SELECT_COUNTRY_ACTIVITY)
class SelectCountryActivity : BaseMvvmActivity<SelectCountryViewModel, BizMeActivitySelectCountryBinding>() {

    private val TAG: String = javaClass.simpleName

    private lateinit var mAdapter:SelectCountryAdapter

    override fun getLayoutId(): Int = R.layout.biz_me_activity_select_country

    override fun getViewModelClass(): Class<SelectCountryViewModel> = SelectCountryViewModel::class.java

    override fun responseVM(action: Int) {}

    override fun initData() {
        mBinding.lifecycleOwner = this
        mBinding.vm = mViewModel
        mBinding.titleView.init(this)
        setSupportActionBar(mBinding.titleView)
    }

    override fun initView() {
        mAdapter = SelectCountryAdapter(arrayListOf(), mListener)
        mBinding.recyclerView.adapter = mAdapter

        val linearLayoutManager = LinearLayoutManager(this)
        mBinding.recyclerView.layoutManager = linearLayoutManager
        //
        showData()
    }

    val mListener = object : SelectCountryAdapter.OnSelectCountryListener {
        override fun onItemClick(b: AreaBean, position: Int) {
            //
            LoggerManager.d(TAG,"onItemClick, position=${position}, data=${b}")
            //
            EventBus.getDefault().post(CountrySelectEvent(b))
            finish()
        }
    }

    fun showData(){
        //
        val data:MutableList<AreaBean> = ReadAssetsJsonUtil.getAreaList("area_code2.json", this)
        mAdapter.list = data
        mAdapter.notifyDataSetChanged()
    }
}